/*
 * $Id: QESubdivision.m 34 2009-04-21 20:24:55Z jon@bway.net $
 * Sleeper
 *
 * Created by Jon Gordon on 3/15/08
 * Copyright Jon Gordon.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#import "QESubdivision.h"

#import "QECountry.h"

@interface QESubdivision (private)
- (void)setName:(NSString *)newName;
- (void)setDesignation:(NSString *)newDesignation;
- (void)setIso3166_2:(NSString *)newIso3166_2;
- (void)setFips:(NSString *)newFips;
- (void)setPostalAbbr:(NSString *)newValue;
- (void)setDivisionOf:(QECountry *)newDivisionOf;
@end


@implementation QESubdivision
- (NSString *)name
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[name retain] autorelease];
} // name

- (NSString *)designation
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[designation retain] autorelease];
} // designation

- (NSString *)iso3166_2
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[iso3166_2 retain] autorelease];
} // iso3166_2

- (NSString *)fips
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[fips retain] autorelease];
} // fips

- (NSString *)postalAbbr
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[postalAbbr retain] autorelease];
} // postalAbbr

- (QECountry *)divisionOf
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[divisionOf retain] autorelease];
} // divisionOf

- (void)dealloc
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    [self setDivisionOf:nil];
    [self setPostalAbbr:nil];
    [self setFips:nil];
    [self setIso3166_2:nil];
    [self setDesignation:nil];
    [self setName:nil];
    
    [super dealloc];
} // dealloc

- (id)initWithName:(NSString *)newName
       designation:(NSString *)newDesignation
         iso3166_2:(NSString *)newIso3166_2
              fips:(NSString *)newFips
        postalAbbr:(NSString *)newPostalAbbr
        divisionOf:(QECountry *)newDivisionOf
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    self = [super init];
    if (nil != self) {
        [self setName:newName];
        [self setDesignation:newDesignation];
        [self setIso3166_2:newIso3166_2];
        [self setFips:newFips];
        [self setPostalAbbr:newPostalAbbr];
        [self setDivisionOf:newDivisionOf];
    } // if
    
    return self;
} // initWithName:designation:iso3166_2:fips:postalAbbr:divisionOf:

- (id)initWithName:(NSString *)newName
       designation:(NSString *)newDesignation
         iso3166_2:(NSString *)newIso3166_2
              fips:(NSString *)newFips
        divisionOf:(QECountry *)newDivisionOf
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithName:newName 
                  designation:newDesignation 
                    iso3166_2:newIso3166_2 
                         fips:newFips 
                   postalAbbr:nil 
                   divisionOf:newDivisionOf];
} // initWithName:designation:iso3166_2:fips:postalAbbr:divisionOf:


- (NSString *)menuItemText
{
    return [NSString stringWithFormat:@"%@ (%@)", [self name], [self postalAbbr]];
} // menuItemText


- (id)copyWithZone:(NSZone *)zone
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    
    QECountry *newData = [[[self class] allocWithZone:zone]
                          initWithName:[self name]
                          designation:[self designation]
                          iso3166_2:[self iso3166_2]
                          fips:[self fips]
                          postalAbbr:[self postalAbbr]
                          divisionOf:[self divisionOf]];
    return newData;
} // copyWithZone:

- (unsigned int)hash
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static const unsigned int HASH_BASE = 17;
    static const unsigned int HASH_FACTOR = 37;
    
    unsigned int result = HASH_BASE;
    result = (result * HASH_FACTOR) + [[self name] hash];
    result = (result * HASH_FACTOR) + [[self designation] hash];
    result = (result * HASH_FACTOR) + [[self iso3166_2] hash];
    result = (result * HASH_FACTOR) + [[self fips] hash];
    result = (result * HASH_FACTOR) + [[self postalAbbr] hash];
    result = (result * HASH_FACTOR) + [[self divisionOf] hash];
    
    return result;
} // hash

- (BOOL)isEqual:(id)anObject
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    
    return ((self == anObject) ||
            ([[self name] isEqual:[anObject name]] &&
             [[self designation] isEqual:[anObject designation]] &&
             [[self iso3166_2] isEqual:[anObject iso3166_2]] &&
             [[self fips] isEqual:[anObject fips]] &&
             [[self postalAbbr] isEqual:[anObject postalAbbr]] &&
             [[self divisionOf] isEqual:[anObject divisionOf]]));
} // isEqual:
@end


@implementation QESubdivision (private)
- (void)setName:(NSString *)newName
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (name != newName) {
        [name autorelease];
        name = [newName copy];
    } // if
} // setName:

- (void)setDesignation:(NSString *)newDesignation
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (designation != newDesignation) {
        [designation autorelease];
        designation = [newDesignation copy];
    } // if
} // setDesignation:

- (void)setIso3166_2:(NSString *)newIso3166_2
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (iso3166_2 != newIso3166_2) {
        [iso3166_2 autorelease];
        iso3166_2 = [newIso3166_2 copy];
    } // if
} // setIso3166_2:

- (void)setFips:(NSString *)newFips
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (fips != newFips) {
        [fips autorelease];
        fips = [newFips copy];
    } // if
} // setFips:

- (void)setPostalAbbr:(NSString *)newValue
{
    // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (postalAbbr != newValue) {
        id tmp = postalAbbr;
        postalAbbr = [newValue copy];
        [tmp release];
    } // if
} // setPostalAbbr:

- (void)setDivisionOf:(QECountry *)newDivisionOf
{
   // NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (divisionOf != newDivisionOf) {
        [divisionOf autorelease];
        divisionOf = [newDivisionOf retain];
    } // if
} // setDivisionOf:
@end
